project(cman_drv)
cmake_minimum_required(VERSION 3.22)
set(CMAN_DRV_DIR "${PROJECT_SOURCE_DIR}")
set(CMAN_DRV_DIR "${PROJECT_SOURCE_DIR}" PARENT_SCOPE)
set(CMAN_DRV_KO "${CMAN_DRV_DIR}/cman.ko")

file(GLOB_RECURSE CMAN_SOURCE_FILES
    "${CMAN_DRV_DIR}/*.c"
    "${CMAN_DRV_DIR}/*.h"
)

if(NOT DEFINED ENABLE_ASAN AND NOT DEFINED ENABLE_COV)
    add_compile_options(-fPIC)
endif()

if (CMAN_DEBUG)
    set(CMAN_MAKE_DEBUG_FLAG DEBUG=1)
endif()

add_custom_command(
    COMMAND make clean
    COMMAND make ${CMAN_MAKE_DEBUG_FLAG}
    DEPENDS ${CMAN_SOURCE_FILES}
    OUTPUT ${CMAN_DRV_KO}
    WORKING_DIRECTORY ${CMAN_DRV_DIR})

add_custom_target(drv
    DEPENDS ${CMAN_DRV_KO}
    WORKING_DIRECTORY ${CMAN_DRV_DIR}
    COMMENT "Compiling cman driver")

add_custom_target(drv-install
    DEPENDS ${CMAN_DRV_KO}
    COMMAND make install
    WORKING_DIRECTORY ${CMAN_DRV_DIR}
    COMMENT "Installing cman driver")